package cn.sunxyz.data.infer;

import cn.sunxyz.common.utils.StringUtil;
import cn.sunxyz.data.infer.parser.NameParser;
import cn.sunxyz.data.infer.parser.conditions.ConditionsParserManager;
import cn.sunxyz.data.infer.parser.reduce.ReduceParser;

/**
 * Created by yangrd on 2017/6/2.
 */
public class DefaultSQLFactory implements SQLFactory {

    private static DefaultSQLFactory sqlFactory = new DefaultSQLFactory();

    private static NameParser reduce = new ReduceParser();

    public static DefaultSQLFactory getSingleton() {
        return sqlFactory;
    }

    // findByName [find] [Name]
    @Override
    public String parser(Class<?> entityCls, Class<?> targetCls, String input, SQLDialect sqlDialect) throws Exception {
        int index = input.indexOf("By");//TODO 目前暂不支持 findNameById这种语句 只支持findByName
        String sql = reduce.parser(entityCls, targetCls, input.substring(0, index).toUpperCase());//获取需要查询属性 目标只支持 删除与查询
        String whereName = input.substring(index + 2, input.length());
        if (StringUtil.isNotEmpty(whereName)) {
            NameParser conditions = ConditionsParserManager.getParserByDialect(sqlDialect);//获取where查询条件
            sql += " where " + conditions.parser(entityCls, whereName);//获取查询过滤条件
        }
        return sql;
    }

}
